<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\ConvertTask;
use App\Jobs\ConvertAwardFormDataToPdfJob;
use Log;

class RetryFailedConvertJob extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'retry-failed-convert-job';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Log::info('开始查找文档转换失败任务');
        $failedTasks = ConvertTask::where('result', '<>', 'success')->where('retry_count', '<', '3')->get();
        $count = count($failedTasks);
        Log::info("共{$count}个失败任务");
        if ($count > 0) {
            foreach ($failedTasks as $task) {
                $task->retry_count = $task->retry_count + 1;
                $task->save();
                ConvertAwardFormDataToPdfJob::dispatch($task);
            }
            Log::info('失败任务已重试');
        }
    }
}
